//
//  YZMRankListView.m
//  YZMicroStock
//
//  Created by wangminhong on 16/5/30.
//  Copyright © 2016年 cqjr. All rights reserved.
//

#import "YZMRankListView.h"

static const float sYZMRankListViewForCellHeight = 40.0;

@interface YZMRankListView()<UITableViewDelegate, UITableViewDataSource>

@end


@implementation YZMRankListView{
    
    UITableView *_tableView;
    UIView *_bgView;
}

- (id)init{
    
    self = [super init];
    
    if (self) {
        
        _dataSource = [NSArray array];
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 1, 1) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.separatorColor = [YZMAppStyle colorForLine];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.scrollEnabled = NO;
        
        [_tableView mas_makeConstraints:^(MASConstraintMaker *maker){
            
            maker.height.mas_equalTo(_dataSource.count * sYZMRankListViewForCellHeight);
        }];
        
        self.customView = _tableView;
        
        _bgView = [UIView new];
        [self insertSubview:_bgView atIndex:0];
        [_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
        
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureAction)];
        [_bgView addGestureRecognizer:tapGesture];
    }
    
    return self;
}

- (void)tapGestureAction{
    
    [self dismissWithAnimationFlag:YES];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *identifier = @"CardChooseCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.backgroundColor = [UIColor whiteColor];
        cell.textLabel.textColor = [YZMAppStyle colorForWhite];
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
        cell.textLabel.font = [YZMAppStyle mediumSizeFont];
    }
    
    cell.textLabel.text = _dataSource[indexPath.section];
    
    return cell;
}


- (void)setDataSource:(NSArray *)dataSource{
    
    _dataSource = dataSource;
//    self.customView = _tableView;
    [_tableView mas_remakeConstraints:^(MASConstraintMaker *maker){
        
        maker.height.mas_equalTo(_dataSource.count * sYZMRankListViewForCellHeight);
    }];
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return [_dataSource count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return sYZMRankListViewForCellHeight;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    if (section == _dataSource.count -1) {
        
        return 0.0;
    }
    
    return 1.0;
}


- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    if (section != _dataSource.count -1) {
        
        UIView *line = [[UIView alloc] init];
        line.backgroundColor = [YZMAppStyle colorForLine];
        return line;
    }
    
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if(self.clickHandle){
        
        self.clickHandle(indexPath.section);
    }
    
    [self dismissWithAnimationFlag:NO];
}

- (void)assignConstaints
{
    self.titleLabel.hidden = YES;
    self.contentLabel.hidden = YES;
    self.footLabel.hidden = YES;
    self.sureButton.hidden = YES;
    self.cancelButton.hidden = YES;
    
    [self.customView mas_makeConstraints:^(MASConstraintMaker *maker){
        
        maker.top.left.right.bottom.centerX.equalTo(_containerView);
    }];
    
    [_containerView mas_makeConstraints:^(MASConstraintMaker *maker){
        
        maker.width.equalTo(self).offset(-80);
        maker.center.equalTo(self);
    }];
}




@end
